/******************************************************************************
 * FileName:        I2C_master.c
 * Dependencies:
 * Processor:       PIC32
 *
 * Complier:        MPLAB C32
 *                  MPLAB IDE
 * Company:         Microchip Technology Inc.
 *
 ********************************************************************/
#include <plib.h>
#include <stdio.h>
#include "Bike_Display_HW_Profile.h"
#include "Bike_Display_I2C_Functions.h"
#include "Bike_Display_4dSystems_Functions.h"
#include "Bike_Display_Bitmap_Definitions.h"
#include "Bike_Display_SystemSetup_Functions.h"
#include "Bike_Display_ADC_Functions.h"
#include "Bike_Display_UART_Functions.h"

// KEY CONCEPT - Set Configuration Bits in code
#pragma config FNOSC    = PRIPLL        // Oscillator Selection
#pragma config FPLLIDIV = DIV_2         // PLL Input Divider (PIC32 Starter Kit: use divide by 2 only)
#pragma config FPLLMUL  = MUL_20        // PLL Multiplier
#pragma config FPLLODIV = DIV_1         // PLL Output Divider
#pragma config FPBDIV   = DIV_1         // Peripheral Clock divisor
#pragma config FWDTEN   = OFF           // Watchdog Timer 
#pragma config WDTPS    = PS1           // Watchdog Timer Postscale
#pragma config FCKSM    = CSDCMD        // Clock Switching & Fail Safe Clock Monitor
#pragma config OSCIOFNC = OFF           // CLKO Enable
#pragma config POSCMOD  = HS            // Primary Oscillator
#pragma config IESO     = OFF           // Internal/External Switch-over
#pragma config FSOSCEN  = OFF           // Secondary Oscillator Enable
#pragma config CP       = OFF           // Code Protect
#pragma config BWP      = OFF           // Boot Flash Write Protect
#pragma config PWP      = OFF           // Program Flash Write Protect
#pragma config ICESEL   = ICS_PGx2      // ICE/ICD Comm Channel Select
#pragma config DEBUG    = OFF           // Debugger Disabled for Starter Kit
            

// application defines


// prototype

// ****************************************************************************
// ****************************************************************************
// Local Support Routines
// ****************************************************************************
// ****************************************************************************



// ****************************************************************************
// ****************************************************************************
// Application Main Entry Point
// ****************************************************************************
// ****************************************************************************

int main(void)
{
    int Status = 0;
    int nETcount = 0;
    int nOTcount = 0;
    int nTPSPercent = 0;
    int nScount = 0;
    int ncount = 0;
    int nColour = GREEN;
    int x1 = 0;
    int y1 = 220;
    int x2 = 0;
    int y2 = 240;
    int colour = 0x07E0;
    int a;

    //int nVersion =
    // Initialize debug messages (when supported)
    DBINIT();
    //SYSTEMConfigWaitStatesAndPB(80000000L);
	
	IO_Startup_Config();
	// initializations
    AnalogStartupConfig( AINPUTS); // initialize the ADC
    
/*    while( 1)
    {
        ScanDigitalSignalsFromBike();
        MeasureBatteryVoltage(); // select the POT input and convert
        MeasureOilTemperature(); // select the POT input and convert
        //DBPRINTF("Analog Input 0 = %d\n", a);
        DelayMs(200);
    }	
    while(1)
	{
        ScanDigitalSignalsFromBike();
    }       
*/
    //UART1Init();
    LCDInit();
    UART5Init();
    EnableInterrupts();
    ECMInit();
        //EMCInit();

	//LCDuSDImage(0, 0, SPLASHSCREEN);
    //Status = LCDWaitTouch(3000);
    //LCDCls();
    
    if (Status == 0x15)
    {
        //DisplaySetup();
        // call normal running function, tbd
    }
    else if (Status == 0x06)
    {
        //LCDuSDImage(0, 0, MainDashRPM);
        while (1)
        {
            MainSetup();
            DelayMs(1000);
        }    
    }    
    MainSetup();   
    LoadFontBMPAddress();

    while(1)
    {
        for(ncount = 0; ncount < 7000; ncount+=100)
        {
            DisplayEngineTemp(nETcount);
            //DisplayOilTemp(nOTcount);
            DisplayTPSPercent(nTPSPercent);
            DisplaySpeed(nScount);
            DisplayRevs(ncount);
            nETcount++;
            nOTcount++;
            nTPSPercent++;
            nScount++;
 
            ScanDigitalSignalsFromBike();
            MeasureBatteryVoltage(); // select the POT input and convert
            MeasureOilTemperature(); // select the POT input and convert
                       
            if(nETcount > 240) nETcount = -80;
            if(nOTcount > 160) nOTcount = -80;
            if(nTPSPercent > 100) nTPSPercent = 0;
            if(nScount > 140) nScount = 0;
        }
        for(; ncount > 0; ncount-=100)
        {
            DisplayRevs(ncount);
            DisplayRevsBar(ncount);
        }
        ECMInit();    
       //ncount = 0;    
    	LCDTouchStatus();
    	LCDGetTouch();
    	//ClearDataBuffer();
    	//DelayMs(1000);
	}
}


/*
    PORTClearBits(IOPORT_D, I2CLEDEnable);

	PCA9635_Init();

	PCA9635_Brightness(ALL, 0x50);

	while (1)
	{
		for(Index = 1; Index <= 32; Index ++)
		{
			PCA9635_LED_On(Index);
			DelayMs(5);
		}
		if (Index == 33)
		{
			for(Index = 32; Index >=1; Index --)
			{
				PCA9635_LED_Off(Index);
				DelayMs(5);
			}
		}
	}
*/


	/*IO_Startup_Config();
	
	while (1)
	{

		PORTSetBits(IOPORT_D, I2CEnable ); //| LCDReset); 

    	PORTSetBits(IOPORT_E, BATLowLED | NeutralLED | LeftTurnLED | RightTurnLED | \
							HighBeamLED | CheckEngineLED | OilPressureLED | LowFuelLED);

		PORTSetBits(IOPORT_G, OilTempLED1 | OilTempLED2 | OilTempLED3 | LightOnRelay);
		
		DelayMs(500);

		PORTClearBits(IOPORT_D, I2CEnable ); //| LCDReset); 

    	PORTClearBits(IOPORT_E, BATLowLED | NeutralLED | LeftTurnLED | RightTurnLED | \
							HighBeamLED | CheckEngineLED | OilPressureLED | LowFuelLED);

		PORTClearBits(IOPORT_G, OilTempLED1 | OilTempLED2 | OilTempLED3 | LightOnRelay);

		DelayMs(500);
	}
	*/


